package com.glorysoft.sunnypda.ui.setting

import android.app.Activity
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.MutableLiveData
import com.glorysoft.sunnypda.R
import com.glorysoft.sunnypda.base.BaseViewModel
import com.glorysoft.sunnypda.base.Constant
import com.glorysoft.sunnypda.base.MyApp
import com.glorysoft.sunnypda.entity.ui.ToastMessageBean
import com.glorysoft.sunnypda.utils.ChangeLanguageHelper
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_SharePreferencesUtils
import java.util.*

/**
 * FileName: SettingViewModel
 * Author: XJH
 * Date: 2022/1/11 18:06
 * Description:
 * History:
 */
class SettingViewModel : BaseViewModel() {

    var mLanguageList = arrayListOf<String>()
    private var mSeverDialog: FreeUI_GeneralFragmentDialog? = null
    private var mFileDialog: FreeUI_GeneralFragmentDialog? = null
    private var mLanguageDialog: FreeUI_GeneralFragmentDialog? = null
    var constant = MutableLiveData<Constant.Companion>()

    fun initVersion(context: Activity){
        constant.value = Constant.Companion
        val manager: PackageManager = context.packageManager
        var info: PackageInfo? = null
        try {
            info = manager.getPackageInfo(context.packageName, 0)
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        val version = info!!.versionName
        constant.value?.VRESION = version

        mLanguageList.clear()
        mLanguageList.add(context.getString(R.string.chinese))
        mLanguageList.add(context.getString(R.string.english))
        mLanguageList.add(context.getString(R.string.vietnamese))
    }


    fun showScanDialog(context: AppCompatActivity){
        mSeverDialog = FreeApi_DialogUtils.getScanDialog(context, context.getString(R.string.input_server_address),
            Constant.REQUEST_URL_DEBUG) {

            constant.value!!.REQUEST_URL_DEBUG = it

            defUI.toastEvent.value = ToastMessageBean(context.getString(R.string.save_success), true)
            FreeApi_SharePreferencesUtils.setSharePre("SUNNY_FILE", "REQUEST_URL_DEBUG", it, MyApp.app)
            mSeverDialog?.dismiss()
        }
        mSeverDialog?.show(context.supportFragmentManager, "WEB_URL_DIALOG")
    }

    fun showFileDialog(context: AppCompatActivity){
        mFileDialog = FreeApi_DialogUtils.getScanDialog(context, context.getString(R.string.input_server_address),
            Constant.FILE_URL) {

            constant.value!!.FILE_URL = it

            defUI.toastEvent.value = ToastMessageBean(context.getString(R.string.save_success), true)
            FreeApi_SharePreferencesUtils.setSharePre("SUNNY_FILE", "FILE_URL", it, MyApp.app)
            mFileDialog?.dismiss()
        }
        mFileDialog?.show(context.supportFragmentManager, "FILE_URL_DIALOG")
    }

    fun updateLanguage(context: AppCompatActivity, language : String){
        mLanguageDialog = FreeApi_DialogUtils.getConfirmAndCancelDialog(context,context.getString(R.string.confirm),
            context.getString(R.string.cancel),context.getString(R.string.change_language_tip), {
                when(language){
                    context.getString(R.string.chinese) ->{
                        ChangeLanguageHelper.changeAppLanguage(context, Locale.SIMPLIFIED_CHINESE,language)

                    }
                    context.getString(R.string.english) ->{
                        ChangeLanguageHelper.changeAppLanguage(context, Locale.ENGLISH,language)

                    }
                    context.getString(R.string.vietnamese) ->{
                        val locale = Locale("vi","Vietnam")
                        ChangeLanguageHelper.changeAppLanguage(context, locale,language)

                    }
                    else ->{
                        ChangeLanguageHelper.changeAppLanguage(context, Locale.SIMPLIFIED_CHINESE,language)
                    }

                }
                ChangeLanguageHelper.restart(context)
            }
        ) { mLanguageDialog!!.dismiss() }
        mLanguageDialog?.show(context.supportFragmentManager, "LANGUAGE_DIALOG")
    }


}